#!/usr/bin/lua
-- Fonspec upgrade file
-- (c) john@phrozen.org, steven@midlink.org gplv2

-- Plugin Information
local NAME    = "threesnakes"            -- Root node of plugin
local HOME    = false                    -- Make plugin root node the new homepage
local TITLE   = "ThreeSnakes"            -- Plugin Title as shown on plugin page
local AUTHOR  = "FonHome Team"           -- Plugin Provider as shown on plugin page
local VERSION = "1.0"                    -- Plugin Version as shown on plugin page

-- Dashboard Information
local DASHBOARD = true                   -- Show plugin on dashboard
local DB_ICON   = "icons/plugins/ts.png" -- Plugin icon file relative to /luci-static/resources
local DB_ORDER  = 101                    -- Plugin dashboard order number


-- Logic
local os = require "os"
local fs = require "luci.fs"
local dir = require "posix".getcwd()

-- Create Plugin object
local plugin = require "luci.fon.pkg".Plugin(NAME)
if not plugin:add(TITLE, AUTHOR, VERSION) then
        print "plugin already installed!"
        os.exit(1)
end

-- Add Dashboard entry
if DASHBOARD then
        plugin:dashboard(DB_ICON, DB_ORDER, NAME)
end

-- Enable home redirect
if HOME then
        plugin:redirect()
end

-- Select files to install
plugin:addfiles(dir.."/files/")

-- Save modes
local modes = {}
for _, file in ipairs(plugin.files) do
        modes[file] = fs.stat(plugin.path .. file, "mode")
end

-- Install files
plugin:finalize()

-- Restore modes
for file, mode in pairs(modes) do
        fs.chmod(file, mode)
end
